package com.jiepos.mpos.inter.fourelement.service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.LogFourElementsDao;
import com.jiepos.mpos.common.entity.App_Log_Four_ElementsEntity;
import com.jiepos.mpos.core.util.CollectionUtil;
import com.jiepos.mpos.core.util.DateUtil;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 四要素通用业务类
 * 
 * @ProjectName: [mpos1]
 * @Package com.jiepos.mpos.core.service
 * @author 肖志伟
 * @version 1.0
 * @since 2017年9月29日 上午11:10:11
 * @history（历次修订内容、修订人、修订时间等
 * @Copyright (c) 2017 江苏杰博实信息技术有限公司
 */
public abstract class AbrFourElementsService {
	private final Logger log = LoggerFactory.getLogger(AbrFourElementsService.class);

	@Autowired
	protected LogFourElementsDao logFourElementsDao;

//	/**
//	 * 获取响应map集合
//	 *
//	 * @param rspCode
//	 *            返回相应码
//	 * @param rspMap
//	 *            返回实体
//	 * @param dataMap
//	 *            要返回的其他数据
//	 * @return
//	 */
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	protected Map getRspMap(String rspCode, Map rspMap, Map dataMap) {
//		if(rspMap == null){
//			rspMap = new HashMap();
//		}
//		Map rspCodeMap = this.getRspCodeListMap();
//		rspMap.put(SystemConstant.RSP_CODE, rspCode);
//		rspMap.put(SystemConstant.RSP_MSG, rspCodeMap.get(rspCode));
//		rspMap.put(SystemConstant.RSP_DATAMAP, dataMap);
//		log.debug("rspMap ===" + rspMap);
//		return rspMap;
//	}

	/**
	 * 获取响应map集合
	 * @param rspCode
	 * @param rspMsg
	 * @param dataMap
	 * @return Map
	 */
	public Map getRspMap(String rspCode,String rspMsg,Map dataMap){
		Map rspMap = new HashMap();
		Map rspCodeMap = this.getRspCodeListMap();
		rspMap.put(SystemConstant.RSP_CODE, rspCode);
		if(StringUtils.isBlank(rspMsg)){
			rspMap.put(SystemConstant.RSP_MSG,rspCodeMap.get(rspCode));
		}else{
			rspMap.put(SystemConstant.RSP_MSG,rspMsg);
		}
		if(dataMap != null){
			rspMap.put(SystemConstant.RSP_DATAMAP,dataMap);
		}else{
			rspMap.put(SystemConstant.RSP_DATAMAP,new HashMap());
		}

		log.debug("客户端响应消息：{}",rspMap);
		return rspMap;
	}

	/**
	 * 获取缓存中的返回给客户端的代码
	 * 
	 * @return Map
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private Map getRspCodeListMap() {
		// 缓存中取
		Map codeListMap = new HashMap();
		List<Map> list = (List<Map>) codeListMap.get("RSP_CODE_MSG");
		Map dataMap = new HashMap();
		Map map = null;
		if(!CollectionUtil.isEmpty(list)){
			for (int i = 0; i < list.size(); i++) {
				map = (Map) list.get(i);
				String code_val = String.valueOf(map.get("code_val"));
				String code_desc = String.valueOf(map.get("code_desc"));
				dataMap.put(code_val, code_desc);
			}
		}
		
		return dataMap;
	}

	/**
	 * 保存四要素认证内容实体
	 * 
	 * @param appLogFourElementsEntity
	 */
	@SuppressWarnings("rawtypes")
	public void saveFourElementsEntity(App_Log_Four_ElementsEntity appLogFourElementsEntity) {
		appLogFourElementsEntity.setFourelement_uuid(IDFactoryUtil.getUUID());
		appLogFourElementsEntity.setLoc_dt(DateUtil.dateToStr(new Date(), "yyyyMMdd"));
		appLogFourElementsEntity.setLoc_ts(DateUtil.dateToStr(new Date(), "HHmmss"));
		appLogFourElementsEntity.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
		appLogFourElementsEntity.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
		appLogFourElementsEntity.setLast_oper_in(SystemConstant.OPER_ADD);
		logFourElementsDao.save(appLogFourElementsEntity);
	}

	/**
	 * 
	 * @param map
	 */
	public App_Log_Four_ElementsEntity getByMap(Map<String, String> map) {
		return logFourElementsDao.findByMultiParams(map);
	}

}
